<template>
  <div class="login-container">
    <div class="login-form-box">
      <div style="flex: 1">
        <img src="../assets/Register.svg" alt="" style="width: 100%">
      </div>
      <div style="flex: 1;display: flex;align-items: center;justify-content: center">
        <el-form :model="user" label-width="100px" style="padding-right: 30px;width: 80%" class="aa" :rules="rules" ref="registerRef">
          <div style="text-align: center; font-weight: bold;font-size: 20px; margin-bottom: 20px">欢迎注册后台管理系统</div>
          <el-form-item prop="username">
            <el-input v-model="user.username" prefix-icon="el-icon-user" placeholder="请输入账号" size="medium"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input v-model="user.password" prefix-icon="el-icon-lock" show-password placeholder="请输入密码" size="medium"></el-input>
          </el-form-item>
          <el-form-item prop="confirmPass">
            <el-input v-model="user.confirmPass" prefix-icon="el-icon-lock" show-password placeholder="请确认密码" size="medium"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="info" style="width: 100%;background-color: #6e77f2" size="medium" @click="register">注 册</el-button>
          </el-form-item>
          <el-form-item>
            <div style="display: flex">
              <div style="flex: 1">已经有账号了？请 <span style="color: #6e77f2;cursor: pointer" @click="$router.push('/login')">登录</span></div>
            </div>
          </el-form-item>
        </el-form>
      </div>

    </div>
  </div>
</template>

<style scoped>
.login-container{
  background-color: #2570c4;
  height: 100vh;
  overflow: hidden;
  display: flex;
  justify-content: center;
  align-items: center;
}

.login-form-box{
  background-color: white;
  width: 50%;
  padding: 40px 0 40px 0;
//height: 45%;
  border-radius: 10px;
  overflow: hidden;
  display: flex;
  align-items: center;
}
.aa >>> .el-form-item__content {
  margin-left:0px !important;
}
</style>

<script>

export default {
  name: "Register",
  data(){
    //验证码校验
    const validatePassword = (rule, confirmPass, callback) => {
      if (confirmPass === '') {
        callback(new Error('请确认密码'));
      } else if (confirmPass !== this.user.password){
        callback(new Error('两次输入密码不一致'));
      }else {
        callback();
      }
    };

    return {
      user:{
        username: '',
        password: '',
        confirmPass: ''
      },
      rules: {
        username: [
          {required: true, message: '请输入账号', trigger: 'blur'},
          {min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur'}
        ],
        confirmPass: [
          { validator: validatePassword, trigger: 'blur' }
        ],

      }
    }
  },

  created() {

  },
  methods: {
    register(){
      this.$refs['registerRef'].validate((valid) => {
        if (valid) {
          //验证通过
          this.$request.post('/register', this.user).then(res => {
            if (res.code == '200'){
              this.$router.push('/login')
              this.$message.success('注册成功')
            } else {
              this.$message.error(res.msg)
            }
          })
        }
      })
    },


  }
}
</script>